package me.zhengjie.ship.shipsoftbase.rest;

import me.zhengjie.aop.log.Log;
import me.zhengjie.ship.shipsoftbase.domain.ShipSoftBase;
import me.zhengjie.ship.shipsoftbase.service.ShipSoftBaseService;
import me.zhengjie.ship.shipsoftbase.service.dto.ShipSoftBaseQueryCriteria;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @author liwh
* @date 2020-04-22
*/
@Api(tags = "软件基础表管理")
@RestController
@RequestMapping("/api/shipSoftBase")
public class ShipSoftBaseController {

    private final ShipSoftBaseService shipSoftBaseService;

    public ShipSoftBaseController(ShipSoftBaseService shipSoftBaseService) {
        this.shipSoftBaseService = shipSoftBaseService;
    }

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('shipSoftBase:list')")
    public void download(HttpServletResponse response, ShipSoftBaseQueryCriteria criteria) throws IOException {
        shipSoftBaseService.download(shipSoftBaseService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询软件基础表")
    @ApiOperation("查询软件基础表")
    @PreAuthorize("@el.check('shipSoftBase:list')")
    public ResponseEntity<Object> getShipSoftBases(ShipSoftBaseQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(shipSoftBaseService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增软件基础表")
    @ApiOperation("新增软件基础表")
    @PreAuthorize("@el.check('shipSoftBase:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody ShipSoftBase resources){
        return new ResponseEntity<>(shipSoftBaseService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改软件基础表")
    @ApiOperation("修改软件基础表")
    @PreAuthorize("@el.check('shipSoftBase:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody ShipSoftBase resources){
        shipSoftBaseService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除软件基础表")
    @ApiOperation("删除软件基础表")
    @PreAuthorize("@el.check('shipSoftBase:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody Integer[] ids) {
        shipSoftBaseService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}